package com.molyfun.parents.activity.schoolonline.homework.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.ResultlistBean;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 * 完成度控件 p+ p- p
 *
 * 可以主动获取选中的id(没选中，返回空)
 * 也可以设置回调监听选中 OnCompleteCheckedChangedCallback
 *
 */
public class CompleteProcessView extends LinearLayout implements View.OnClickListener{

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.complete_checkbox_layout)
    LinearLayout mCompleteCheckboxLayout;

    private String mCurrentChooseId;

    private List<ResultlistBean> mResultlistBeanList;

    public CompleteProcessView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public CompleteProcessView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CompleteProcessView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_complete_process, this);
        ButterKnife.inject(this);
    }

    /**
     * init view
     *
     */
    public void initData(List<ResultlistBean> resultlistBeans) {
        this.mResultlistBeanList = resultlistBeans;
        if(resultlistBeans != null){
            mCompleteCheckboxLayout.removeAllViews();
            for(ResultlistBean resultlistBean : resultlistBeans){
                View completeCellView = LayoutInflater.from(context).inflate(R.layout.layout_complete_cell, null, false);
                ImageView checkIcon = (ImageView)completeCellView.findViewById(R.id.check_icon);
                TextView checkDesc = (TextView)completeCellView.findViewById(R.id.checkbox_name);
                if(resultlistBean.isIsselected()){
                    mCurrentChooseId = resultlistBean.getId();
                    checkIcon.setImageResource(R.mipmap.ic_checkbox_checked);
                }else{
                    checkIcon.setImageResource(R.mipmap.ic_checkbox_unchecked);
                }
                checkDesc.setText(resultlistBean.getTypename());
                completeCellView.setTag(resultlistBean.getId());
                completeCellView.setOnClickListener(this);
//                LinearLayout.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//                layoutParams.weight = 1f;
                mCompleteCheckboxLayout.addView(completeCellView);
            }
        }
    }

    private OnCompleteCheckedChangedCallback mOnCompleteCheckedChangedCallback;

    public void setOnCompleteCheckedChangedCallback(OnCompleteCheckedChangedCallback onCompleteCheckedChangedCallback){
        this.mOnCompleteCheckedChangedCallback = onCompleteCheckedChangedCallback;
    }

    public static interface OnCompleteCheckedChangedCallback{
        public void onCheckedChanged(String id);
    }

    public String getChooseId(){
        return mCurrentChooseId;
    }

    @Override
    public void onClick(View v) {

        String chooseId = (String)v.getTag();
        if(!TextUtils.isEmpty(chooseId)){

            for(ResultlistBean resultlistBean : mResultlistBeanList){
                resultlistBean.setIsselected(false);
                if(resultlistBean.getId().equals(chooseId)){
                    resultlistBean.setIsselected(true);
                    mCurrentChooseId = resultlistBean.getId();
                }
            }

            initData(mResultlistBeanList);

            if(mOnCompleteCheckedChangedCallback != null){
                mOnCompleteCheckedChangedCallback.onCheckedChanged((String)v.getTag());
            }

        }

    }

}